//
//  huafeiViewController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/6/11.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "RechargeController.h"

#import "InputPhoneView.h"
#import "ChooseMoneyView.h"
#import "PayModeView.h"
#import "JudgePhoneNumberBelong.h"
#import "OrderPaymentController.h"

#define BelongLabelText @"手机号码归属地"
#define ErrorBelongLabelText @"请输入正确的手机号码"


@interface RechargeController ()<ChooseMoneyViewDelegate,UITextFieldDelegate,PayModeViewDelegate>
@property(nonatomic,strong)XRCarouselView *XrCarouselView;

@property (nonatomic,strong)UITextField *phoneNumberTextField;

@property (nonatomic,strong)UILabel *belongLabel;

@property(nonatomic,copy)NSString *chooseMoney;

@end

@implementation RechargeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BACKGROUND_COLOR;
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self addViews];
}

- (void)addViews
{
    [self.view addSubview:self.XrCarouselView];
    
    //适配
    CGFloat margin = 5;
    CGFloat inputCount;
    CGFloat chooseCount;
    if (iPhone5) {
        inputCount = 10;
        chooseCount = 7;
    }else {
        inputCount = 9;
        chooseCount = 6;
    }
    
    InputPhoneView *inputView = [[InputPhoneView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(self.XrCarouselView.frame)+margin,WIDTH, HEIGHT/inputCount)];
    self.phoneNumberTextField = inputView.phoneNumText;
    self.phoneNumberTextField.delegate = self;
    [self.phoneNumberTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.belongLabel = inputView.belongLabel;
    [self.view addSubview:inputView];
    
    
    ChooseMoneyView *chooseView = [[ChooseMoneyView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(inputView.frame)+margin, WIDTH, HEIGHT/chooseCount)];
    [self.view addSubview:chooseView];
    chooseView.delegate = self;
    
    PayModeView *payView = [[PayModeView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(chooseView.frame), WIDTH,HEIGHT-self.XrCarouselView.Height-chooseView.Height- inputView.Height-margin*3)];
    payView.delegate = self;
    [self.view addSubview:payView];
}


- (void)textFieldDidChange:(UITextField *)textField
{
    
    if (textField == self.phoneNumberTextField) {
        if (textField.text.length >=11) {
            textField.text = [textField.text substringToIndex:11];
        }
        if (textField.text.length >10) {
            NSLog(@"调方法");
            [self resignResponderTextField];
            [self receiveMessage:self.phoneNumberTextField.text];
        } else {
            self.belongLabel.text = BelongLabelText;
        }
    }
    
}

- (void)receiveMessage:(NSString *)phoneStr {
    NSString *account = phoneStr;
    
    NSDictionary *param = @{@"account":[NSString stringWithFormat:@"%@", account]};
    
    [[NetWorkSharedTools sharedNetWorkTools] GetObjectWithURLDict:param AndMenthodName:@"Index/getGS/" completeBlock:^(id object) {
        
        if ([JudgePhoneNumberBelong isMobileNumber:account]) {
            self.belongLabel.text = [object objectForKey:@"message"];
        } else {
            self.belongLabel.text = ErrorBelongLabelText;
        }
    }];
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignResponderTextField];
}

- (void)resignResponderTextField {
    [self.phoneNumberTextField resignFirstResponder];
}

#pragma mark >>>>>>>>>>ChooseMoneyView代理
- (void)ChooseMoneyViewDidClick:(UIButton *)button
{
    
    [self resignResponderTextField];
    
    if (button.tag == 1000) {
        self.chooseMoney = @"30";
    }else if (button.tag == 1001){
        self.chooseMoney = @"50";
    }else{
        self.chooseMoney = @"100";
    }
}

//立即支付
- (void)payModeViewDidClick
{
    OrderPaymentController *OrderPaymentVC = [[OrderPaymentController alloc] init];
    [self.navigationController pushForm:self.navigationController pushToAndHidden:OrderPaymentVC];
}


#pragma mark>>>>>>>>>>懒加载轮播
- (XRCarouselView *)XrCarouselView
{
    if (!_XrCarouselView) {
        NSArray *imageArr = @[
                              [UIImage imageNamed:@"vn2.jpg"],
                              [UIImage imageNamed:@"vn3.jpg"],
                              [UIImage imageNamed:@"vn1.jpg"]
                              ];
        _XrCarouselView = [XRCarouselView carouselViewWithFrame:CGRectMake(0,0, WIDTH, HEIGHT/6) imageArray:imageArr];
        _XrCarouselView.time = 2;
    }
    return _XrCarouselView;
}

@end
